package cn.edu.cjw.APMFP.ui.dialog;

import cn.edu.cjw.APMFP.ProductionTask.Service.TaskService;
import cn.edu.cjw.APMFP.ProductionTask.Service.TaskServiceImpl;
import cn.edu.cjw.APMFP.ProductionTask.pojo.ProductionTask;
import cn.edu.cjw.APMFP.Util.CacheUtil;
import cn.edu.cjw.APMFP.Util.DateUtil;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.ui.component.combobox.MultiComboBox;
import cn.edu.cjw.APMFP.ui.component.date.Chooser;
import cn.edu.cjw.APMFP.ui.table.TableCallBack;
import cn.edu.cjw.APMFP.user.Service.UserService;
import cn.edu.cjw.APMFP.user.Service.UserServiceImpl;
import cn.edu.cjw.APMFP.user.pojo.User;
import javax.swing.*;
import java.awt.*;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Stream;


public class ProductionTaskDialog extends JDialog {

	private JButton confirmBtn = new JButton("确认");
	private JButton cancelBtn = new JButton("取消");

	private TaskService service = new TaskServiceImpl();

	private ProductionTask entity;

	private UserService userService = new UserServiceImpl();

	private List<User> userList = new ArrayList<>();

	private TableCallBack callBack;

	public ProductionTaskDialog(ProductionTask entity, TableCallBack callBack){
		this.entity = entity;
		this.callBack = callBack;
		this.setTitle(entity != null ? "编辑" : "新增");
		this.setSize(500, 500);
		this.init();
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
//		this.pack();
		this.setVisible(true);
	}

	private void init(){
		this.setLayout(new BorderLayout());

		try {
			userList.addAll(userService.showAll());
		} catch (Exception e) {
			e.printStackTrace();
		}

		GridBagLayout gridBagLayout = new GridBagLayout();
		JPanel centerPanel = new JPanel(gridBagLayout);
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.ipady = 10;

		JLabel idLabel = new JLabel("编号:");
		JTextField idField = new JTextField(27);
		constraints.gridx = 0;
		constraints.gridy = 0;
		centerPanel.add(idLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(idField, constraints);

		JLabel descLabel = new JLabel("备注:");
		JTextArea descField = new JTextArea(3, 27);
		constraints.gridx = 0;
		constraints.gridy = 1;
		centerPanel.add(descLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(descField, constraints);

		JLabel chargeLabel = new JLabel("负责人:");
		JComboBox<String> chargeField = new JComboBox<>();
		userList.forEach(u -> {
			chargeField.addItem(u.getName());
		});
		chargeField.setPreferredSize(idField.getPreferredSize());
		constraints.gridx = 0;
		constraints.gridy = 2;
		centerPanel.add(chargeLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(chargeField, constraints);

		JLabel dateLabel = new JLabel("完成日期:");
		JTextField dateField = new JTextField();
		Chooser ser = Chooser.getInstance(DateUtil.DATE_STR);
		ser.register(dateField);
		dateField.setPreferredSize(idField.getPreferredSize());
		constraints.gridx = 0;
		constraints.gridy = 3;
		centerPanel.add(dateLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(dateField, constraints);

		JLabel stepLabel = new JLabel("进度:");
		MultiComboBox stepField = new MultiComboBox();
		stepField.setPreferredSize(idField.getPreferredSize());
		constraints.gridx = 0;
		constraints.gridy = 4;
		centerPanel.add(stepLabel, constraints);
		constraints.gridx = 1;
		centerPanel.add(stepField, constraints);

		if(entity != null){
			HashMap<String, Boolean> percentageMap = entity.getPercentage();
			if(percentageMap != null){
				List<String> keyList = new ArrayList<>();
				List<String> keySelectedList = new ArrayList<>();
				entity.getPercentage().forEach((k, v) -> {
					keyList.add(k);
					if(Boolean.TRUE.equals(v)){
						keySelectedList.add(k);
					}
				});
				stepField.setValues(keyList.toArray());
				stepField.setSelectValues(keySelectedList.toArray());
			}
			idField.setText(entity.getTaskId());
			idField.setEditable(false);
			descField.setText(entity.getDescribe());
			chargeField.setSelectedItem(entity.getChargeMan());
			dateField.setText(DateUtil.format(entity.getCompletionTime(), DateUtil.DATE_STR));
		}

		this.add(centerPanel, BorderLayout.CENTER);

		JPanel footerPanel = new JPanel();
		footerPanel.setPreferredSize(new Dimension(this.getWidth(), 100));
		footerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 10));
		footerPanel.add(confirmBtn);
		footerPanel.add(cancelBtn);
		confirmBtn.addActionListener(e -> {
			String id = idField.getText().trim();
			if(!StringUtil.isNotBlank(id)){
				JOptionPane.showMessageDialog(null, "id不能为空");
				return;
			}
			String desc = descField.getText().trim();
			if(!StringUtil.isNotBlank(desc)){
				JOptionPane.showMessageDialog(null, "desc不能为空");
				return;
			}
			int index = chargeField.getSelectedIndex();
			if(index < 0){
				JOptionPane.showMessageDialog(null, "请选择负责人");
				return;
			}
			String chargeUser = userList.get(index).getName();
			String date = dateField.getText().trim();
			if(!StringUtil.isNotBlank(date)){
				JOptionPane.showMessageDialog(null, "请选择完成日期");
				return;
			}
			Date finishTime = null;
			try {
				finishTime = new Date(DateUtil.parse(date, DateUtil.DATE_STR).getTime());
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null, "日期格式不正确");
				return;
			}
			try {
				if(entity == null){
					User user = CacheUtil.getUser();
					String username = user.getName();
					Date now = new Date(System.currentTimeMillis());
					service.addTask(id, username, now, chargeUser, finishTime, desc);
				}else{
					Object[] selectList = stepField.getSelectedValues();
					HashMap<String, Boolean> percentPage =  entity.getPercentage();
					if(percentPage != null){
						Stream.of(selectList).forEach(s -> {
							percentPage.put(s.toString(), true);
						});
					}
					service.updateTask(entity, chargeUser, finishTime, percentPage);
				}
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null, "操作失败");
				return;
			}
			if(callBack != null){
				callBack.refreshTable();
			}
			JOptionPane.showMessageDialog(null, "操作成功");
			close();
		});
		cancelBtn.addActionListener(e -> {
			close();
		});
		this.add(footerPanel, BorderLayout.SOUTH);
	}
	
	private void close(){
		this.dispose();
	}
	
	public static void main(String[] args) {
		new ProductionTaskDialog(null, null);
	}

}
